﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TemplateGenerator.Controls;

namespace TemplateGenerator.Forms
{
    public partial class ImportWindow : Form
    {
        public ImportWindow()
        {
            InitializeComponent();
            fieldsNValues = new DataSet();
            this.dataGridView1.DataSource = new DataTable();
            this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

        }

        public delegate void ImportHandler(object sender, DataSet values);
		public event ImportHandler OnImport;

		public DataSet fieldsNValues { get; set; }

        private void OnLoad_Click(object sender, EventArgs e)
        {

            ImportExcel import = new ImportExcel();

            //open a dialog to let the user select the desired file
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select file";
            fdlg.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
            fdlg.FilterIndex = 1;


            //if the user selects a file this will be imported to the program.
            if (fdlg.ShowDialog() == DialogResult.OK)
            {

                this.dataGridView1.Columns.Clear();
                fieldsNValues = import.Import(fdlg.FileName);
                this.dataGridView1.DataSource = fieldsNValues.Tables[0];
                this.dataGridView1.AutoResizeColumns();
                this.dataGridView1.Update();
                this.dataGridView1.Refresh();
            }
        }

        private void OnClose_Click(object sender, EventArgs e)
        {
            OnImport(this, fieldsNValues);
            this.Close();
        }

        private void OnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addColumnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Initialize the button column.
            DataGridViewTextBoxColumn buttonColumn = new DataGridViewTextBoxColumn();
            // Add the button column to the control.
            dataGridView1.Columns.Add(buttonColumn);

        }

        private void removeColumnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewColumn column in this.dataGridView1.SelectedColumns)
                this.dataGridView1.Columns.Remove(column);
        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                RenameColumn rename = new RenameColumn();

                if (rename.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    dataGridView1.Columns[e.ColumnIndex].HeaderText = rename.Text;
                    dataGridView1.Columns[e.ColumnIndex].Name = rename.Text;
                }

            }
        

        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataObject d = dataGridView1.GetClipboardContent();
            Clipboard.SetDataObject(d);

        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
